﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMS.Model;
using System.Data;
using System.Data.SqlClient;
using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
   public class StockInData
    {
       private readonly string sp_GetEntInfoByTm ="GetEntInfoByTM";
       private readonly string sp_InsertEntList = "StockInEntLsInsert";
       private readonly string sp_GetITHPOSByBils = "GetEntAllStockInfoByBil";
       private readonly string sp_GetITDPOS_DetailByBils = "ITDPOSDetailGetByBils";
       private readonly string sp_GetUserDefaultCofigInfo = "GetUserDefaultCofigInfo";
       private readonly string sp_GetExistTmInITHPOSCn = "ExistTmInStock";
       private readonly string sp_GetWHSInfoBysupId = "GetWHSInfoBysupId ";

       //入库
       public string StockInList(string entList, User use)
       {
           SqlConnection con = new SqlConnection(DbHelperSQL.ConStr);
           SqlCommand cmd = new SqlCommand(sp_InsertEntList,con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@entList",entList);
           cmd.Parameters.AddWithValue("@userName", use.UserName);
           cmd.Parameters.AddWithValue("@shipName", use.CII);
           cmd.Parameters.AddWithValue("@shipChiName", use.CIIDES);
           cmd.Parameters.AddWithValue("@warehouse", use.WHSSWHS);
           string bilCode = string.Empty;
           SqlParameter bilPar = cmd.Parameters.Add("@bilCode", SqlDbType.VarChar,30);
           bilPar.Value = string.Empty;
           bilPar.Direction = ParameterDirection.Output;
           con.Open();
           try
           {
              // DbHelperSQL.ExecuteStoredProcNonQuery(cmd);
               cmd.ExecuteNonQuery();
               bilCode = bilPar.Value.ToString();
              // con.Close();
           }
           catch //(Exception e)
           {
               if (con.State == ConnectionState.Open)
                   con.Close();
               throw;
           }
          
           return bilCode;
       }

       //获取条码信息
       public DataTable GetEntInfoByTm(string tm)
       {
           SqlParameter[] parameters = {
				new SqlParameter("@TM", SqlDbType.VarChar, 200)
			};
           parameters[0].Value =tm;
           DataTable dt = new DataTable();
           dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_GetEntInfoByTm, parameters).Tables[0];

           return dt;
       }
       
       public List<StockInModel> BuildStockInModel(string tm)
       {
           List<StockInModel> ls = new List<StockInModel>();
           DataTable dt = GetEntInfoByTm(tm);
           foreach (DataRow dr in dt.Rows)
           {
               StockInModel md = new StockInModel();
               md.Tm = dr["Tm"].ToString();
               md.Mh = dr["Mh"].ToString();
               md.Price =  (float)Convert.ToDouble(dr["Price"].ToString());
               md.ProductCode =dr["ProductCode"].ToString();
               md.Qty = (float)Convert.ToDouble(dr["Qty"].ToString());
               md.Total = (float)Convert.ToDouble(dr["total"].ToString());
               md.Color = dr["Color"].ToString();
               md.Date = DateTime.Now;
               ls.Add(md);
           }
           return ls;
       }

       //获取入库单信息
       public DataTable GetILIPOSInfoByBil(string bilCode)
       {
           SqlParameter[] parameters = {
				new SqlParameter("@bilCode", SqlDbType.VarChar, 200)
			};
           parameters[0].Value = bilCode;
           DataTable dt = new DataTable();
           dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_GetITHPOSByBils, parameters).Tables[0];

           return dt;
       }

       //根据入库单号，获取明细
       public DataTable GetPosBilDetailInfoData(string bil)
       {
           SqlParameter[] parameters = {
				new SqlParameter("@bilCode", SqlDbType.VarChar, 200)
			};
           parameters[0].Value = bil;
           DataTable dt = new DataTable();
           dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_GetITDPOS_DetailByBils, parameters).Tables[0];
           return dt;
       }

       /// <summary>
       /// 获取用户登录后的默认信息
       /// </summary>
       /// <param name="loginId"></param>
       /// <returns></returns>
       public DataTable GetUserDefaultInfoData(string loginId)
       {
           
          SqlParameter[] parameters = {
				new SqlParameter("@uid", SqlDbType.VarChar, 100)
			};
           parameters[0].Value = loginId;
           DataTable dt = new DataTable();
           dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_GetUserDefaultCofigInfo, parameters).Tables[0];

           return dt;
       }

       //获取条码信息是否存在
       public bool GetExistTmInITHPOSCn(string tm)
       {
           SqlParameter[] parameters = {
				new SqlParameter("@tm", SqlDbType.VarChar, 100)
			};
           parameters[0].Value = tm;

           int cnt = Convert.ToInt32(DbHelperSQL.ExecuteStoredProcScalar(sp_GetExistTmInITHPOSCn, parameters));

          return cnt>0;
       }

       //获取店铺仓库信息
       public DataTable GetWHSInfoBysupIdData(string supId)
       {
           SqlParameter[] parameters = {
				new SqlParameter("@supId", SqlDbType.VarChar, 100)
			};
           parameters[0].Value = supId;
           DataTable dt = new DataTable();
           dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_GetWHSInfoBysupId, parameters).Tables[0];

           return dt;
       }
    }
}
